package com.zb.jnlxc.service;

import com.ZLHW.base.Exception.BaseErrorModel;
import com.ZLHW.base.Form.Page;
import com.ZLHW.base.dao.QueryCondition;
import com.ZLHW.base.service.BaseService;
import com.zb.jnlxc.constants.ProcessConstants;
import com.zb.jnlxc.dao.*;
import com.zb.jnlxc.form.JiYaDetail;
import com.zb.jnlxc.model.*;
import com.zb.jnlxc.model.Mould.MODEL_STATUS;

import com.zb.util.MapUtils;
import com.zb.util.StringUtils;
import org.jbpm.api.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-8-16
 * Time: 下午4:11
 * To change this template use File | Settings | File Templates.
 */
@Transactional
@Service
public class MouldFlowService extends BaseService<MouldDAO,Mould, Integer> {
    private static final Logger log = LoggerFactory.getLogger(MouldFlowService.class);

    @Resource
    private SchemeDAO schemeDao;
    @Resource
    private FlowService flowService;
    @Resource
    private PaiChanMouldDAO paiChanMouldDAO;
    @Resource
    private PaichanOrderDetailDAO paichanOrderDetailDAO;
    @Resource
    private MouldFixTaskDAO mouldFixTaskDAO;
    @Resource
    private MouldDanHuaTaskDAO mouldDanHuaTaskDAO;
    @Resource
    private MouldPaoPaiTaskDAO mouldPaoPaiTaskDAO;
    @Resource
    private MouldBaoFeiTaskDAO mouldBaoFeiTaskDAO;
    @Resource
    private ProductTeamDAO productTeamDAO;
    @Resource
    private PaiChanRecordDAO paiChanRecordDAO;
    @Resource
    private ProductDetailDAO productDetailDAO;
    @Resource
    private ProductProcessDAO productProcessDAO;
    @Resource
    private LiaoKuangDAO liaoKuangDAO;
    @Resource
    private ProductDAO productDAO;
    @Resource
    private NotifyFlowService notifyFlowService;
    @Resource
    DataDictService dataDictService;
    @Resource
    AdminService adminService;
    
    /**
     * 获取转修任务
     * @return
     */
    public Page loadMouldFixTask(Page page,Admin admin){
        List<QueryCondition> queryConditions=new ArrayList();
        StringBuffer hql=new StringBuffer("from MouldFixTask t where 1=1 and isProcessed = false " );
//                "and t.mould.fixer=:RefFixer ");
//        queryConditions.add(new QueryCondition("RefFixer",admin));
        hql.append(" order by ").append(page.getSortKey()).append(" ").append(page.getSortOrder());
        mouldFixTaskDAO.findByPageWithTmpHQL(page, hql.toString(), queryConditions);
        return page;
    }

    /**
     * 获取拋排任务
     * @return
     */
    public Page loadMouldPaoPaiTask(Page page){
        List<QueryCondition> queryConditions=new ArrayList();
        StringBuffer hql=new StringBuffer("from MouldPaoPaiTask t where 1=1 and isProcessed = false ");
        hql.append("order by ").append(page.getSortKey()).append(" ").append(page.getSortOrder());
        mouldFixTaskDAO.findByPageWithTmpHQL(page, hql.toString(), queryConditions);
        return page;
    }

    /**
     * 获取氮化任务
     * @return
     */
    public Page loadMouldDanHuaTask(Page page){
        List<QueryCondition> queryConditions=new ArrayList();
        StringBuffer hql=new StringBuffer("from MouldDanHuaTask t where 1=1 and isProcessed = false ");
        hql.append("order by ").append(page.getSortKey()).append(" ").append(page.getSortOrder());
        mouldFixTaskDAO.findByPageWithTmpHQL(page, hql.toString(), queryConditions);
        return page;
    }

    /**
     * 异常模转修处理
     */
    public void ycmzxcl(Integer mouldFixTaskId,Map<String,String> form,String nextStep,Admin admin) {
        MouldFixTask mouldFixTask = mouldFixTaskDAO.getById(mouldFixTaskId);
        Mould mould = mouldFixTask.getMould();
        mould.setLastTask("异常模转修处理");
        String zjxc = form.get("zjxc");
        String productTeamId = form.get("productTeamId");
        if(StringUtils.isEmpty(zjxc)||
                StringUtils.isEmpty(productTeamId))
            throw new BaseErrorModel("请求参数异常","");
        ProductTeam productTeam = productTeamDAO.loadById(Integer.parseInt(productTeamId));
        mould.setProductTeam(productTeam);
        mould.setZjxc(Integer.parseInt(zjxc));
        if("氮化".equals(nextStep)){
            mould.setCurrentTask("氮化");
            mould.setProcessingScheme("");
            danHua(mould);
        }else if ("上机".equals(nextStep)){//抛排，上机
            mould.setCurrentTask("抛排收模处理");
            mould.setProcessingScheme(nextStep);
            paoPai(mould);
        }else if ("合格入库".equals(nextStep)){//抛排，上机
            mould.setCurrentTask("抛排收模处理");
            mould.setProcessingScheme(nextStep);
            paoPai(mould);
        }else if("报废".equals(nextStep)){
            mould.setProcessingScheme("");
            mould.setCurrentTask("报废退模处理");
            baoFei(mould);
        }
        update(mould);
        mouldFixTask.setIsProcessed(true);
        mouldFixTask.setOperator(admin);
        mouldFixTaskDAO.update(mouldFixTask);
    }

    /**
     * 氮化处理 从哪里来，回哪里去
     * @param dbId
     * @param map
     * @param admin
     */
    public void dhcl(Integer dbId,Map<String,String> map,Admin admin){
        SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        MouldDanHuaTask mouldDanHuaTask = mouldDanHuaTaskDAO.getById(dbId);
        mouldDanHuaTask.setIsProcessed(true);
        String startTime = map.get("startTime");
        String finishTime = map.get("finishTime");
        String danHuaLuHao = map.get("danHuaLuHao");
        if(StringUtils.isNotEmpty(startTime)){
            try {
                mouldDanHuaTask.setCreateDate(dateformat1.parse(startTime.replace("T"," ")));
            } catch (ParseException e) {
                throw new BaseErrorModel("氮化进炉时间解析失败","");
            }
        }
        if (StringUtils.isNotEmpty(finishTime)){
            try {
                mouldDanHuaTask.setFinishTime(dateformat1.parse(finishTime.replace("T"," ")));
            } catch (ParseException e) {
                throw new BaseErrorModel("氮化进炉时间解析失败","");
            }
        }
        if(StringUtils.isNotEmpty(danHuaLuHao)){
            mouldDanHuaTask.setDanHuaLuHao(danHuaLuHao);
        }
        Mould mould = mouldDanHuaTask.getMould();
        //增加限产
        mould.setSyxc(mould.getSyxc()+mould.getZjxc());

        String source = mouldDanHuaTask.getSource();
        mould.setLastTask("异常模转修处理");
        if("异常模转修处理".equals(source)){
            mould.setCurrentTask("异常模转修处理");
            fixMould(mould,mould.getProductTeam());
        }else if ("抛排收模处理".equals(source)){
            mould.setCurrentTask("抛排收模处理");
            paoPai(mould);
        }
        update(mould);
        mouldDanHuaTask.setOperator(admin);
        mouldDanHuaTaskDAO.update(mouldDanHuaTask);
    }

    /**
     * 抛排收模处理
     */
    public void ppsmcl(Integer dbId,Map<String,String> map,String nextStep,Admin admin){
        MouldPaoPaiTask mouldPaoPaiTask = mouldPaoPaiTaskDAO.getById(dbId);
        Mould mould = mouldPaoPaiTask.getMould();
        String productTeamId = map.get("productTeamId");
        ProductTeam productTeam;
        if(StringUtils.isBlank(productTeamId)){
            throw new BaseErrorModel("机台不能为空");
        }else {
            productTeam = productTeamDAO.getById(Integer.parseInt(productTeamId));
        }

        mould.setProductTeam(productTeam);
        mould.setLastTask("抛排收模处理");
        mould.setProcessingScheme("");
        if("氮化".equals(nextStep)){
            mould.setCurrentTask("氮化");
            danHua(mould);
        }else if ("转修".equals(nextStep)){//抛排，上机
            mould.setCurrentTask("异常模转修处理");
            fixMould(mould,productTeam);
        }else if ("上机".equals(nextStep)){//抛排，上机
            mould.setCurrentTask("挤压班组收模");
            notifyShouMU(mould,admin);
        }else if ("合格入库".equals(nextStep)){//抛排，上机
            mould.setCurrentTask("抛排收模处理");
            notifyStorage(mould,admin);
        }
        update(mould);
        mouldPaoPaiTask.setOperator(admin);
        mouldPaoPaiTask.setIsProcessed(true);
        mouldPaoPaiTaskDAO.update(mouldPaoPaiTask);
    }
    
    //退模报废处理
    public void tmbfcl(Integer dbId,Map<String,String> map,String nextStep,Admin admin){
        MouldBaoFeiTask mouldBaoFeiTask = mouldBaoFeiTaskDAO.getById(dbId);
        Mould mould = mouldBaoFeiTask.getMould();
        if("报废".equals(nextStep)){
            mould.setStatus(Mould.MODEL_STATUS.彻底报废.getValue());
        }else if("退模".equals(nextStep)){
            mould.setStatus(Mould.MODEL_STATUS.已退模.getValue());
            notifyAddMould(mould,admin);
        }else if("返修".equals(nextStep)){
            mould.setStatus(Mould.MODEL_STATUS.定制.getValue());
            startmouldFlowByKey(mould);
        }
        update(mould);
        mouldBaoFeiTask.setOperator(admin);
        mouldBaoFeiTask.setIsProcessed(true);
        mouldBaoFeiTaskDAO.update(mouldBaoFeiTask);
    }

    /**
     * 仓库发模
     */
    public void ckfm(String taskId,Map<String,String> map,Admin admin){
        Mould mould = getMouldFlowInfo(taskId);
        mould.setLastTask("仓库发模");
        paoPai(mould);
        flowService.completeTask(taskId,admin);
    }
    
    /**
     * 用于前台获取的模具信息
     * @param taskId
     * @return
     */
    public Mould getMouldFlowInfo(String taskId){
        Integer mouldId=(Integer)flowService.getContentMap(taskId, "mouldId");
        return this.getById(mouldId);
    }
    
    /**
     * 挤压班组收模
     */
    public void jybzsm(String taskId, Admin user,Map maps,String nextStep) {
        Mould mould = getMouldFlowInfo(taskId);
        flowService.completeTask(taskId, user);
        mould.setLastTask("挤压班组收模");
        if("还库".equals(nextStep)){
            mould.setCurrentTask("抛排收模处理");
            paoPai(mould);
        }else {
            mould.setCurrentTask("挤压试模使用");
            mould.setStatus(MODEL_STATUS.已收模.getValue());
        }
        update(mould);
    }
    
    /**
     * 挤压试模具使用
     */
    public void jysmsy( Admin user,Integer mouldId,Integer paichanRecordId,Map<String,String> form,List<JiYaDetail> jiYaDetails){
        PaiChanRecord paiChanRecord = paiChanRecordDAO.getById(paichanRecordId);
        if(paiChanRecord.isFinished()||paiChanRecord.getOrderForm().isFinished()){
            throw new BaseErrorModel("本次排产已完成或人为结束");
        }
        if(paiChanRecord.isJiYaFinished()){
            throw new BaseErrorModel("该排产已挤压完成");
        }
        Mould mould = getById(mouldId);
        //记录挤压记录
        Product product = new Product();
        product.setCreateDate(new Date());
        //挤压工艺
        product.setMjwd(form.get("mjwd"));//模具温度
        product.setLbwd(form.get("lbwd"));//铝棒温度
        product.setClwd(form.get("clwd"));//出料温度
        product.setJysd(form.get("jysd"));//挤压速度
        product.setLsl(form.get("lsl"));  //拉伸力
        product.setLbgg(form.get("lbgg"));//铝棒规格
        product.setLbcd(form.get("lbcd"));//铝棒长度
        product.setLbsl(form.get("lbsl"));//铝棒数量
        productDAO.create(product);

        //本次挤压的总米数，需要在剩余限产中减去
        int totalMeter = 0;
        //保存模具生产记录
        for(JiYaDetail jiYaDetail : jiYaDetails){
            if(jiYaDetail.getCompQuantity()<=0){
                continue;
            }
            //校验请求参数
            jiYaDetail.validate();

            ProductDetail productDetail = new ProductDetail();
            //修改料框状态
            PaichanOrderDetail paichanOrderDetail = paichanOrderDetailDAO.getByPaichanSize(jiYaDetail.getPaichanSize(), paichanRecordId);
            if(paichanOrderDetail==null) {
                throw new BaseErrorModel("系统异常","");
            }
            String steps = paichanOrderDetail.getPaichanRecord().getOrderForm().getSteps();
            LiaoKuang liaoKuang = getLiaoKuangByCode(jiYaDetail.getLiaoKuangCode(), steps);

            productDetail.setPaichanOrderDetail(paichanOrderDetail);
            productDetail.setProduct(product);
        	productDetail.setLiaoKuang(liaoKuang);
            productDetail.setMould(mould);
            productDetail.setCompQuantity(jiYaDetail.getCompQuantity());
            productDetail.setCompWeight(jiYaDetail.getCompWeight());
            productDetail.setCompTotalMeter(jiYaDetail.getCompTotalMeter());
            productDetail.setCreateDate(new Date());
            totalMeter+=productDetail.getCompTotalMeter();
            productDetailDAO.create(productDetail);

            ProductProcess productProcess = new ProductProcess();
            productProcess.setProductDetail(productDetail);
            productProcess.setProcessName(ProcessConstants.JIYA);
            productProcess.setUncompQuantity(productDetail.getCompQuantity());
            productProcess.setCompQuantity(0);
            productProcess.setQuantity(productDetail.getCompQuantity());
            productProcess.setRealWeight(productDetail.getCompWeight());
            productProcess.setLastProcessId(0);
            productProcess.setLiaoKuang(liaoKuang);
            productProcess.setOperator(user);
            productProcess.setFinished(false);
            productProcess.setCreateDate(new Date());
            productProcessDAO.create(productProcess);

            paichanOrderDetail.setCompWeight(paichanOrderDetail.getCompWeight()+productDetail.getCompWeight());
            paichanOrderDetail.setCompQuantity(paichanOrderDetail.getCompQuantity()+productDetail.getCompQuantity());
            int compTotalMeter = (paichanOrderDetail.getCompQuantity()*paichanOrderDetail.getSetSize())/1000;
            paichanOrderDetail.setCompTotalMeter(compTotalMeter);
            paichanOrderDetailDAO.update(paichanOrderDetail);
        }
        paiChanRecordDAO.updateAfterJiYa(paichanRecordId);

        //获取卸模原因
        String xmyy = form.get("xmyy");

        if("不卸模".equals(xmyy)){
        	log.info("不卸模，继续试模具，模具号:{}",mould.getDbId());
        }else{
        	log.info("卸模,模具号:{}",mould.getDbId());
        	mould.setStatus(MODEL_STATUS.泡模收模确认.getValue());
        	notifyPaoMu(mould,user);
            mould.setXmyy(xmyy);
        }
        mould.setSyxc(mould.getSyxc()-totalMeter);
        mould.setLjcl(mould.getLjcl()+totalMeter);
        update(mould);
    }

    private LiaoKuang getLiaoKuangByCode(String code,String steps){
        //修改料框状态
        LiaoKuang liaoKuang = liaoKuangDAO.getBycode(code);
        if(liaoKuang==null){
            throw new BaseErrorModel("料框编号不存在");
        }

        if(liaoKuang.getUsed()&&
                !"过磅".equals(liaoKuang.getCurrentStep())){
            throw new BaseErrorModel("料框:"+liaoKuang.getCode()+" 未处于过磅状态，无法一起处理","");
        }
        if(liaoKuang.getUsed()&&
                StringUtils.isNotBlank(liaoKuang.getSteps())){
            if(!liaoKuang.getSteps().equals(steps)){
                throw new BaseErrorModel("胚料与料框号:"+liaoKuang.getCode()+" 处理流程不一致，无法一起处理","");
            }
        }

        liaoKuang.setCurrentStep(ProcessConstants.GUOBANG);
        liaoKuang.setSteps(steps);
        liaoKuang.setUsed(true);
        liaoKuangDAO.update(liaoKuang);
        return liaoKuang;
    }
    
    /**
     * 机台选择模具试模
     */
    public void selectMouldToShiMo(Integer paiChanMouldId){
        PaiChanMould paoChanMould = paiChanMouldDAO.getById(paiChanMouldId);
        Mould mould = paoChanMould.getMould();
        String id = mould.getCode()+"_"+mould.getMouldRecordCount();
        ProcessInstance processInstance = flowService.getExecutionService().createProcessInstanceQuery().processInstanceKey(id).uniqueResult();
        flowService.getExecutionService().signalExecutionById(processInstance.getId());
        mould.setStatus(MODEL_STATUS.试模.getValue());
        update(mould);
    }
    

    /**
     * 通知收模
     * @param mould
     */
    public void notifyShouMU(Mould mould,Admin admin){
    	SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMddHHmmss");
        int currentCount = mould.getMouldRecordCount();
        mould.setMouldRecordCount(currentCount+1);
        String id = mould.getCode()+"_"+mould.getMouldRecordCount();
        String notifyId = id+"-"+dateformat1.format(new Date());
        Map map = new HashMap();
        map.put("mouldId", mould.getDbId());
        notifyFlowService.startNotify(notifyId, admin.getAccount(), "", "挤压班组收模", "/content/flow/newmould/jybzsm.vm" , "挤压班组收模",map);
        update(mould);
    }
    
    /**
     * 通知泡模收模确认
     * @param mould
     */
    public void notifyPaoMu(Mould mould,Admin admin){
    	SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMddHHmmss");
        int currentCount = mould.getMouldRecordCount();
        mould.setMouldRecordCount(currentCount+1);
        String id = mould.getCode()+"_"+mould.getMouldRecordCount();
        String notifyId = id+"-"+dateformat1.format(new Date());
        Map map = new HashMap();
        map.put("mouldId", mould.getDbId());
        notifyFlowService.startNotify(notifyId, admin.getAccount(), "", "泡模收模确认", "/content/flow/newmould/pmsmqr.vm" , "泡模收模确认",map);
        update(mould);
    }
    
    /**
     * 通知报废退模
     * @param mould
     */
    public void notifyTuiMu(Mould mould,Admin admin){
    	SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMddHHmmss");
        int currentCount = mould.getMouldRecordCount();
        mould.setMouldRecordCount(currentCount+1);
        mould.setStatus(MODEL_STATUS.报废处理.getValue());
        String id = mould.getCode()+"_"+mould.getMouldRecordCount();
        String notifyId = id+"-"+dateformat1.format(new Date());
        Map map = new HashMap();
        map.put("mouldId", mould.getDbId());
        notifyFlowService.startNotify(notifyId, admin.getAccount(), "", "报废退模处理", "/content/flow/newmould/bftmcl.vm" , "报废退模处理",map);
        update(mould);
    }

    /**
     * 模具入库提醒
     * @param mould
     */
    public void notifyStorage(Mould mould,Admin admin){
        SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMdd-HHmmss");
        String notifyId = mould.getCode()+"_"+dateformat1.format(new Date());
        Map map = new HashMap();
        map.put("mouldId", mould.getDbId());
        map.put("hkr",admin.getName());
        notifyFlowService.startNotify(notifyId, admin.getAccount(), "", "合格入库", "/content/flow/newmould/hgrk.vm" , "合格入库",map);
    }

    /**
     * 通知仓库发模
     * @param mould
     */
    public void notifyDistribute(Mould mould,Admin admin){
        SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMdd-HHmmss");
        String notifyId = mould.getCode()+"_"+dateformat1.format(new Date());
        Map map = new HashMap();
        map.put("mouldId", mould.getDbId());
        notifyFlowService.startNotify(notifyId, admin.getAccount(), "", "仓库发模", "/content/flow/newmould/ckfm.vm" , "仓库发模",map);
    }

    /**
     * 通知添加模具
     * @param mould
     */
    public void notifyAddMould(Mould mould,Admin admin){
        SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMdd-HHmmss");
        String notifyId = mould.getCode()+"_"+dateformat1.format(new Date());
        Map map = new HashMap();
        map.put("schemeId", mould.getScheme().getDbId());
        notifyFlowService.startNotify(notifyId, admin.getAccount(), "", "添加模具", "/content/flow/scheme/startMould.vm" , "添加模具");
    }
    
    /**
     * 模具入库
     */
    public void mjrk(String taskId, Admin user,Map map){
    	Mould mould = getMouldFlowInfo(taskId);
    	mould.setStatus(MODEL_STATUS.合格入库.getValue());
    	update(mould);
    	flowService.completeTask(taskId, user);
    }

    /**
     * 泡模收模确认
     * @param taskId
     * @param user
     * @param map
     */
    public void pmsmqr(String taskId, Admin user,Map map){
    	Mould mould = getMouldFlowInfo(taskId);
    	update(mould);
        mould.setLastTask("泡模收模确认");
        mould.setCurrentTask("抛排收模处理");
    	flowService.completeTask(taskId, user);
    	paoPai(mould);
    }
    

    /**
     * 开启模具订单流程
     * @return
     */
    public ProcessInstance startmouldFlowByKey(Mould mould){
        SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMddHHmmss");
        String id = mould.getCode()+"-"+dateformat1.format(new Date());
        Map map = new HashMap();
        map.put("mouldId", mould.getDbId());
        logger.info("开启模具流程.id={}:",id);
        return flowService.startProcessInstanceByKey("newMould",id,map);
    }

    /**
     * 转修处理
     * @param mould
     */
    public void fixMould(Mould mould,ProductTeam productTeam){
    	mould.setStatus(MODEL_STATUS.转修处理.getValue());
    	update(mould);
        MouldFixTask mouldFixTask = new MouldFixTask();
        mouldFixTask.setSource(mould.getLastTask());
        mouldFixTask.setMould(mould);
        mouldFixTask.setCreateDate(new Date());
        mouldFixTask.setIsProcessed(false);
        mouldFixTask.setSyxc(mould.getSyxc() == null ? 0 : mould.getSyxc());
        mouldFixTask.setZjxc(0);
        mouldFixTask.setProductTeam(productTeam);
        mouldFixTaskDAO.create(mouldFixTask);
    }

    /**
     * 抛排处理
     */
    public void paoPai(Mould mould){
    	mould.setStatus(MODEL_STATUS.抛排收模处理.getValue());
    	update(mould);
        MouldPaoPaiTask mouldPaoPaiTask = new MouldPaoPaiTask();
        mouldPaoPaiTask.setSource(mould.getLastTask());
        mouldPaoPaiTask.setMould(mould);
        mouldPaoPaiTask.setCreateDate(new Date());
        mouldPaoPaiTask.setIsProcessed(false);
        mouldPaoPaiTaskDAO.create(mouldPaoPaiTask);
    }

    /**
     * 氮化处理
     * @param mould
     */
    public void danHua(Mould mould){
    	mould.setStatus(MODEL_STATUS.氮化.getValue());
    	update(mould);
        MouldDanHuaTask mouldDanHuaTask = new MouldDanHuaTask();
        mouldDanHuaTask.setSource(mould.getLastTask());
        mouldDanHuaTask.setMould(mould);
        mouldDanHuaTask.setCreateDate(new Date());
        mouldDanHuaTask.setIsProcessed(false);
        mouldDanHuaTaskDAO.create(mouldDanHuaTask);
    }

    /**
     * 报废处理
     * @param mould
     */
    public void baoFei(Mould mould){
    	mould.setStatus(MODEL_STATUS.报废处理.getValue());
    	update(mould);
        MouldBaoFeiTask mouldBaoFeiTask = new MouldBaoFeiTask();
        mouldBaoFeiTask.setSource(mould.getLastTask());
        mouldBaoFeiTask.setMould(mould);
        mouldBaoFeiTask.setCreateDate(new Date());
        mouldBaoFeiTask.setIsProcessed(false);
        mouldBaoFeiTaskDAO.create(mouldBaoFeiTask);
    }
    
    /**
     * 模具入厂验收
     */
    public void mjrcys(String taskId, Admin user,Map maps,String nextStep) {
    	Mould mould = getMouldFlowInfo(taskId);
    	if("报废退模".equals(nextStep))
    		baoFei(mould);
        mould.setWaijingOk(MapUtils.getBoolen(maps,"waijingOk","外径不能为空"));
        mould.setZongHouOk(MapUtils.getBoolen(maps,"zongHouOk","总厚不能为空"));
        mould.setZhiKouZhiJingOk(MapUtils.getBoolen(maps,"zhiKouZhiJingOk","止口直径不能为空"));
        mould.setZhiKouShengDuOk(MapUtils.getBoolen(maps,"zhiKouShengDuOk","止口深度不能为空"));
        mould.setWaiXinDaoJiaoOk(MapUtils.getBoolen(maps,"waiXinDaoJiaoOk","外形倒角不能为空"));
        mould.setMuJuBianMaOk(MapUtils.getBoolen(maps,"muJuBianMaOk","模具编码不能为空"));
        mould.setShaoDingOk(MapUtils.getBoolen(maps,"shaoDingOk","销钉不能为空"));
        mould.setTiMuKongOk(MapUtils.getBoolen(maps,"tiMuKongOk","提模孔不能为空"));
        mould.setYingDuOk(MapUtils.getBoolen(maps,"yingDuOk","提模孔不能为空"));
        update(mould);
    	flowService.completeTask(taskId,nextStep, user);
    }
    
    /**
     * 模具入厂责任人分配
     */
    public void mjrczrrfp(String taskId, Admin user,Map<String,String> form,String nextStep) {
        String fixerId = form.get("fixerId");
        String productTeamId = form.get("productTeamId");
    	Mould mould = getMouldFlowInfo(taskId);
        ProductTeam productTeam = productTeamDAO.getById(Integer.parseInt(productTeamId));
        mould.setProductTeam(productTeam);
        mould.setFixer(adminService.loadById(Integer.parseInt(fixerId)));
        mould.setLastTask("入厂责任人分配");
        this.update(mould);
    	fixMould(mould,productTeam);
    	flowService.completeTask(taskId,nextStep, user);
    }

    public void leaveFactory(Integer mouldId, String mjqx) {
        Mould mould = getById(mouldId);
        if(mould.getStatus()==MODEL_STATUS.转修处理.getValue()){
            throw new BaseErrorModel("已经是转修处理了!");
        }
        mould.setMjqx(mjqx);
        mould.setMcomment(mould.getMcomment()+"\r\n"+mould.getMjqx());
        fixMould(mould,null);
    }
}
